/*
 * Copyright 2009 Nominet UK
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 * Derivative work produced by Richard Barnes under terms of Apache License
 */

package com.googlecode.ecritdroid;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.util.Log;

public class ECRITNotificationUtil {

	/* Log tag */
	static private final String TAG = "ECRITNotificationUtil";

	/* Class variables */
	static private boolean mEnable = false;
	static private boolean mMobile = false;
	static private boolean mReady = false;

	static public synchronized boolean checkConnectivity(Context context) {

		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		mEnable = prefs.getBoolean(ECRITPrefs.ECRIT_PREF_ENABLE, false);
		mMobile = prefs.getBoolean(ECRITPrefs.ECRIT_PREF_MOBILE, false);
		mReady = false;


		// ECRIT good to go if we have LoST mappings and an IP connection we're allowed to use
		ConnectivityManager mCmgr = (ConnectivityManager) context
		.getSystemService(Service.CONNECTIVITY_SERVICE);

		NetworkInfo ni = mCmgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		boolean wifiReady = ((ni != null) && (ni.isConnected()));

		ni = mCmgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		boolean cellularReady = ((ni != null) && ni.isConnected() && (ni.getSubtype() >= TelephonyManager.NETWORK_TYPE_EDGE));

		// Ready to go if we have LoST mappings and an IP link we're allowed to use
		mReady = LoSTService.hasMappings() && (wifiReady || (mMobile && cellularReady));
		
		Log.d(TAG, "Updating connectivity state: enabled="+ mEnable +" mobile="+ mMobile +" mappings="+ LoSTService.hasMappings() +" ready="+ mReady);

		return mReady;
	}

	static public boolean updateNotification(Context context) {

		// check the current system status
		checkConnectivity(context);

		// clicking this will open the prefs pane
		PendingIntent pi = PendingIntent.getActivity(context, 0,
				new Intent(context, ECRITMain.class), 0);

		NotificationManager mNmgr = (NotificationManager) context
		.getSystemService(Service.NOTIFICATION_SERVICE);

		int text, icon;
		if (mReady && mEnable) {
			text = R.string.notify_ecrit_enabled;
			icon = R.drawable.ic_ecrit_on;
		} else if (mEnable) {
			text = R.string.notify_ecrit_offline;
			icon = R.drawable.ic_ecrit_offline;
		} else {
			text = R.string.notify_ecrit_disabled;
			icon = R.drawable.ic_ecrit_off;
		}

		String str1 = context.getString(text);
		String str2 = context.getString(R.string.notify_summary);

		Notification n = new Notification();
		n.icon = icon;
		n.flags |= Notification.FLAG_ONGOING_EVENT;
		n.when = 0;
		n.setLatestEventInfo(context, str1, str2, pi);
		mNmgr.notify(0, n);

		return mReady;
	}
}
